package com.wwdx.controller.admin;

import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxBookClassifyModel;
import com.wwdx.utils.ConstantHolder;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.Serializable;
import java.util.Date;

/**
 * @author liguoliang
 * @date 2018/5/17 17:48
 */
@Controller
@RequestMapping("/system/wwdx_book_classify")
public class WwdxBookClassifyController extends BaseController{

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ModelAndView list(WwdxBookClassifyModel model,
                                    @RequestParam(value = "page", defaultValue = "1") int page,
                                    @RequestParam(value = "rows",defaultValue = "10") int rows) {

        ModelAndView mav = new ModelAndView();
        mav.addObject("listSize", wwdxBookClassifyService.selectModelCount(model));
        mav.addObject("list", wwdxBookClassifyService.selectModelList(model,page,rows));
        return mav;
    }

    /**
     * 新增
     */
    @RequestMapping("/create")
    public ModelAndView create(WwdxBookClassifyModel model) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("model",model);
        return mv;
    }

    /**
     * 保存新增
     */
    @RequestMapping("/save")
    public String save(WwdxBookClassifyModel model, RedirectAttributes attributes) {
        Integer type = model.getType();
        //添加标签
        if (type == null) {
            model.setPid(0L);
            model.setType(0);
        }
        model.setStatus(0);
        model.setGmtCreated(new Date());
        model.setGmtModified(new Date());
        Serializable id = wwdxBookClassifyService.save(model);
        String key = String.format(ConstantHolder.BOOK_CLASSIFY_LIST, id);
        redisTemplate.delete(key);
        attributes.addAttribute("pid",model.getPid());
        attributes.addAttribute("type",model.getType());
        return "redirect:/system/wwdx_book_classify/list";
    }

    /**
     * 修改页面
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Long id) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("model", wwdxBookClassifyService.getById(id));
        return mav;
    }

    /**
     * 保存修改
     */
    @RequestMapping("/update")
    public String update(WwdxBookClassifyModel model, RedirectAttributes attributes) {
        model.setGmtModified(new Date());
        wwdxBookClassifyService.update(model);
        String key = String.format(ConstantHolder.BOOK_CLASSIFY_LIST, model.getId());
        redisTemplate.delete(key);
        attributes.addAttribute("pid",model.getPid());
        attributes.addAttribute("type",model.getType());
        return "redirect:/system/wwdx_book_classify/list";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresRoles("super")
    public String delete(Long id, RedirectAttributes attributes) {
        WwdxBookClassifyModel model = wwdxBookClassifyService.getById(id);
        wwdxBookClassifyService.delete(model);
        String key = String.format(ConstantHolder.BOOK_CLASSIFY_LIST, id);
        redisTemplate.delete(key);
        attributes.addAttribute("pid",model.getPid());
        attributes.addAttribute("type",model.getType());
        return "redirect:/system/wwdx_book_classify/list";
    }
}